package com.competition.oss.utils;

import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;

public class OcrUtils {

    public static final String APP_ID = "23970352";
    public static final String API_KEY = "ULw9xlXiPt2tjak4qjOCxpzq";
    public static final String SECRET_KEY = "VzmezIfQAIlougggTCVTqhYcRV2oQPoh";

//    public static JSONObject getOcrJSON(String url){
//        // 初始化一个AipOcr
//        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
//
//
//        // 可选：设置网络连接参数
//        client.setConnectionTimeoutInMillis(2000);
//        client.setSocketTimeoutInMillis(60000);
//        // 调用接口
//        String path = url;
//        JSONObject res = client.webimageLoc(path,new HashMap<String, String>());
//        return res;
//    }

    public static JSONObject getOcrJSON(MultipartFile image) throws IOException {
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);


        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        byte[] file = image.getBytes();
        // 调用接口
        JSONObject res = client.vatInvoice(file, new HashMap<>());
        System.out.println(res.toString(2));
        return res;
    }



    //https://computer-competition2021.oss-cn-beijing.aliyuncs.com/2021/04/21/56b023b5-a097-4c05-b896-95c76cee8d564c402c86585a0616681878dc6ff0ccf2.jpeg
}
